package com.sos.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sos.entity.LayuiR;
import com.sos.entity.PieData;
import com.sos.entity.po.Account;
import com.sos.entity.po.Doctor;
import com.sos.mapper.AccountMapper;
import com.sos.mapper.DoctorMapper;
import com.sos.service.DoctorService;
import com.sos.util.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 港某人
 * @date 2021/3/20 12:28
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Object> implements DoctorService {

    @Resource
    AccountMapper accountMapper;

    @Override
    public LayuiR<Doctor> info(String token) {
        DecodedJWT decode = null;
        try {
            decode = JWT.decode(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (decode == null) {
            return LayuiR.data(null, "token失效");
        }
        if (TokenUtil.isTokenOutTime(decode)) {
            return LayuiR.data(null, "token过期");
        }
        String id = decode.getClaim("id").asString();
        Doctor doctor = baseMapper.getDoctorById(id);
        return LayuiR.data(doctor);
    }

    @Override
    public void updateInfo(Doctor doctor) {
        HttpServletRequest request = TokenUtil.getRequest();
        if (request != null) {
            String token = request.getHeader("x-token");
            String s = TokenUtil.decryptToken(token, "id");
            if (s != null) {
                doctor.setObjId(s);
            }
        }
        baseMapper.updateInfo(doctor);
    }

    @Override
    public void updatePassword(Account account) {
        accountMapper.updateAccount(account);
    }

    @Override
    public List<PieData> getPie1() {
        return baseMapper.getPie1(TokenUtil.getDoctorId());
    }

    @Override
    public List<PieData> getPie2() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        String startDate = year + "-" + monthValue + "-1";
        LocalDate plusMonths = now.plusMonths(1);
        year = plusMonths.getYear();
        monthValue = plusMonths.getMonthValue();
        String endDate = year + "-" + monthValue + "-1";
        return baseMapper.getBar1(TokenUtil.getDoctorId(), startDate, endDate);
    }

    @Override
    public List<PieData> getBar1() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        String startDate = year + "-1-1";
        LocalDate plusMonths = now.plusYears(1);
        year = plusMonths.getYear();
        String endDate = year + "-1-1";
        return baseMapper.getBar1(TokenUtil.getDoctorId(), startDate, endDate);
    }

    public static void main(String[] args) {

    }


}
